//
//  HistoryOrderDetailController.m
//  FactorySale
//
//  Created by 主事丫环HU on 2020/8/4.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "HistoryOrderDetailController.h"
#import "HistoryOrderDetailHeaderView.h"
#import "HistoryOrderDetailCell.h"
#import "UnpaidOrHistoryOrderInfoDetailModel.h"
#define NibWithClass(Class)  [UINib nibWithNibName:NSStringFromClass([Class class]) bundle:nil]
#define StringWithClass(Class) NSStringFromClass([Class class])
@interface HistoryOrderDetailController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topNavBarConstraint;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) UnpaidOrHistoryOrderInfoDetailModel *detailModel;
@end

@implementation HistoryOrderDetailController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self requestSendOrderDetailInfo];
}
- (void)setupUI{
    self.view.backgroundColor = Main_BackGroundColor;
    self.page = 1;
    self.dataArray = [NSMutableArray arrayWithCapacity:0];
    [self.tableView registerNib:NibWithClass(HistoryOrderDetailHeaderView) forHeaderFooterViewReuseIdentifier:StringWithClass(HistoryOrderDetailHeaderView)];
    self.tableView.rowHeight = 90;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestSendOrderDetailInfo)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
- (void)requestSendOrderDetailInfo{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"id"] = self.id;
    param[@"pageNumber"] = @1;
    [NetWorkTool POST:unpaidSendOrderInfo param:param success:^(id dic) {
        self.detailModel = [UnpaidOrHistoryOrderInfoDetailModel mj_objectWithKeyValues:dic[@"data"]];
        self.dataArray = [UnpaidOrHistoryRelListModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"relList"]];
        
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        self.page = 2;
        self.dataArray = [self.dataArray mutableCopy];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        [self.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        
    } fail:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } needUser:YES];
}
- (void)loadMoreData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"pageNumber"] = @(self.page);
    param[@"id"] = self.id;
    [NetWorkTool POST:unpaidSendOrderInfo param:param success:^(id dic) {
        NSMutableArray *modelArray = [UnpaidOrHistoryRelListModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"relList"]];
        if (modelArray.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        self.page++;
        [self.dataArray addObjectsFromArray:modelArray];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } fail:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } needUser:YES];
}
#pragma mark - tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (([self.detailModel.logistics_name isKindOfClass:[NSNull class]] || [self.detailModel.logistics_name isEqual:[NSNull null]] || self.detailModel.logistics_name == nil || [self.detailModel.logistics_name isEqualToString:@""]) &&([self.detailModel.bank_name isKindOfClass:[NSNull class]] || [self.detailModel.bank_name isEqual:[NSNull null]] || self.detailModel.bank_name == nil || [self.detailModel.bank_name isEqualToString:@""])){// 没有物流公司也没有收款账户
        return 230;
    } else if ((![self.detailModel.logistics_name isEqualToString:@""]) && ([self.detailModel.bank_name isKindOfClass:[NSNull class]] || [self.detailModel.bank_name isEqual:[NSNull null]] || self.detailModel.bank_name == nil || [self.detailModel.bank_name isEqualToString:@""])) {// 有物流公司 没有收款账户
        return 300;
    } else if ((![self.detailModel.bank_name isEqualToString:@""]) && ([self.detailModel.logistics_name isKindOfClass:[NSNull class]] || [self.detailModel.logistics_name isEqual:[NSNull null]] || self.detailModel.logistics_name == nil || [self.detailModel.logistics_name isEqualToString:@""])){// 有收款账户 没有物流公司
        return 270;
    } else {// 都有数据
        return 340;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    HistoryOrderDetailHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:StringWithClass(HistoryOrderDetailHeaderView)];
    headerView.statusImageView.image = [UIImage imageNamed:@"对方已付款"];
    headerView.statusLabel.text = @"对方已付款";
    headerView.model = self.detailModel;
    return headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}
#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UnpaidOrHistoryRelListModel *model = self.dataArray[indexPath.row];
    HistoryOrderDetailCell *cell = [HistoryOrderDetailCell cellWithTableView:tableView];
    cell.model = model;
    return cell;
}
- (IBAction)backButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
